
package com.sm.shoplook.service.suit.impl;

import java.util.List;

import com.sm.shoplook.domain.suit.Suit;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sm.shoplook.constant.ResultMessage;
import com.sm.shoplook.dao.mapper.suit.SuitMapper;
import com.sm.shoplook.service.suit.SuitService;
import com.sm.shoplook.vo.base.ApiException;
import com.sm.shoplook.vo.base.ApiResult;
import com.sm.shoplook.vo.suit.back.req.SuitSearchReqVo;
import com.sm.shoplook.vo.suit.front.resp.SuitPageRespVo;

@Service
public class SuitServiceImpl implements SuitService {

	private static final Logger LOGGER = LoggerFactory.getLogger(SuitServiceImpl.class);
	@Autowired
	private SuitMapper suitMapper;

	@Override
	public ApiResult<PageInfo<SuitPageRespVo>> selectSuitListPage(SuitSearchReqVo suitSearchReqVo) {
		PageHelper.startPage(suitSearchReqVo.getOffset(), suitSearchReqVo.getPageSize());
		List<SuitPageRespVo> suitRespVos = suitMapper.selectSuitListPage(suitSearchReqVo);
		return new ApiResult<>(ResultMessage.RESULT_SUCCESS_1, new PageInfo<>(suitRespVos));
	}

	@Override
	public ApiResult<SuitPageRespVo> selectSuitById(Integer id) {
		SuitSearchReqVo suitSearchReqVo = new SuitSearchReqVo();
		suitSearchReqVo.setSuitId(id);
		List<SuitPageRespVo> suitPageRespVos = suitMapper.selectSuitListPage(suitSearchReqVo);
		if (CollectionUtils.isEmpty(suitPageRespVos)) {
			return new ApiResult<>(ResultMessage.SUIT_NOT_EXISTS);
		}
		return new ApiResult<>(ResultMessage.RESULT_SUCCESS_1, suitPageRespVos.get(0));
	}

	@Override
	public ApiResult<Integer> batchDelete(String[] ids) throws ApiException {
		int i = suitMapper.batchDelete(ids);
		if (i > 0) {
			return new ApiResult<>(ResultMessage.RESULT_SUCCESS_1);
		}
		throw new ApiException();
	}

	@Override
	public ApiResult<Integer> updateAuditSuit(Suit suit) throws ApiException {
		int i = suitMapper.updateByPrimaryKeySelective(suit);
		if (i < 1) {
		    throw new ApiException();
		}
		return new ApiResult<>(ResultMessage.RESULT_SUCCESS_1);
	}
}
